<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal='false' fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="dialogFormVisible = false">关闭页面</el-button>
    <el-form ref="dataForm" :model="dataForm" label-width="80px" :rules="rules" :disabled="display">
      <el-row>
        <el-col :span="6">
          <el-form-item label="学校类型" prop="schoolType">
            <el-select v-model="dataForm.schoolType">
              <el-option v-for="item in schoolTypeOptions" :value="item.value" :label="item.label" :key="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年级名称" prop="gradeName">
            <el-input class="input-width-217" v-model="dataForm.gradeName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="备注" prop="memo">
          <el-input type="textarea" v-model="dataForm.memo"></el-input>
        </el-form-item>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer" v-if="!display">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit()">提 交</el-button>
    </div>
    <search-school v-if="schoolVisible" ref="schoolDialog" @refreshForm="getSchoolData"></search-school>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/pvm/grade'
  import { fetchSchoolList } from '@/api/pvm/school'
	import { remote } from '@/api/admin/dict'
  import SearchSchool from '../teacher/components/SearchSchool'


  export default {
    name: 'add',
    data() {
      return {
        title: '',
        type: '',
        schoolTypeOptions: [],
        display: false,
        dialogFormVisible: false,
        dataForm: {
          schoolType: '',
          gradeName: '',
          // classCount: '',
          memo: ''
        },
        rules: {
          schoolType: [{required: true,message: '请选择学校类型',trigger: 'blur'}],
          gradeName: [{required: true,message: '请输入年级名称',trigger: 'blur'}],
          // classCount: [{required: true,message: '请输入班级数量',trigger: 'blur'}]
        },
        schoolVisible: false
      }
    },
    components: {
      SearchSchool
    },
    methods: {
      /*
       * params {id} ID，不存在时为新增
       * params {type} 表单类型 "add":新增 "edit":编辑 "check":查看
       */
      init(id, type) {
        this.type = type
        remote('school_type').then(response => {
          this.schoolTypeOptions = response.data.data
        })
        this.dialogFormVisible = true;
        if(type == 'check'){
          this.display = true
        }else{
          this.display = false
        }
        //  根据type确定弹窗标题
        switch(type){
          case 'add': {
            this.title = '年级新增'
            break;
          };
          case 'edit': {
            this.title = '年级编辑'
            break;
          };
          case 'check': {
            this.title = '年级详情'
            break;
          };
        }
        //  初始化表单信息
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
        this.$set(this.dataForm,'id',id)
        //  获取年级信息
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if(this.dataForm.id){
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataForm.schoolType *= 1
                fetchSchoolList(this.dataForm.schoolId).then(result => {
                  this.$set(this.dataForm,'schoolName',result.data.data.schoolName)
                })
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      //提交按钮
      onSubmit: function() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$set(this.dataForm,'sortNum',0)
            if(this.dataForm.id){
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.dialogFormVisible = false
                  this.$message.success('编辑成功')
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              }).catch(() => {
                alert('编辑失败，请联系管理员')
              })
            }else{
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.dialogFormVisible = false
                  this.$message.success('添加成功')
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              }).catch(() => {
                alert("添加失败，请联系管理员")
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 检查老师编号
      checkTeacherCode(val){
        checkTeacherCode(this.dataForm.schoolId,this.dataForm.teacherCode).then(response => {
          let responseData = response.data.data
          if(responseData == 'false'){
            this.$message.warning('该教师编号已存在，请重新输入')
            this.$set(this.dataForm,'teacherCode','')
          }
        })
      },

      // 打开学校弹窗
      openSchoolList() {
        this.schoolVisible = true
        this.$nextTick(() => {
          this.$refs.schoolDialog.init()
        })
      },
      // 获取选择学校信息
      getSchoolData(data){
        this.$set(this.dataForm,'schoolName',data.schoolName)
        this.$set(this.dataForm,'schoolId',data.id)
        this.$set(this.dataForm,'schoolType',data.schoolType)
      }
    }
  }
</script>
